class NestedIterator {
private:
    stack<NestedInteger> sints;
public:
    NestedIterator(vector<NestedInteger> &nestedList) {
        for(auto iter = nestedList.rbegin(); iter != nestedList.rend(); iter++)
            sints.push(*iter);
    }

    int next() {
        auto p = sints.top();
        sints.pop();
        return p.getInteger();
    }

    bool hasNext() {
        while(!sints.empty()){
            auto p = sints.top();
            if(p.isInteger())
                return true;
            sints.pop();

            vector<NestedInteger> nested_List = p.getList();
            for(auto iter = nested_List.rbegin(); iter != nested_List.rend(); iter++)
                sints.push(*iter);
        }

        return false;
    }
};